<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Communal Meals Component Administrator Controller
 */
class CommunalMealsController extends JController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 * @access public
	 */
	public function __construct() {
		parent::__construct();
	}
	
	/**
	 * Method to save an item
	 */
	public function save() {
		$viewName = JRequest::getVar('view', NULL);
		switch ($viewName) {
			case "editevent":
				$this->saveEvent();
				break;
			case "signup":
				$this->saveSignUpList();
				break;
			case "signupauto":
				$this->saveAutoSignUpList();
				break;
			case "signupreport":
				$this->saveEventFromSignUpReport();
				break;
		}
	}
	
	/**
	 * Method to save an event
	 * (only permitted for crew members)
	 */
	private function saveEvent() {
		global $option;
		// Save event
		$event = new cmeals_std_class();
		$event->id = JRequest::getVar('event_id', NULL);
		$event->group_id = JRequest::getVar('group_id', NULL);
		$event->status = JRequest::getVar('status', NULL);
		$event->description_html = JRequest::getVar('description_html', NULL, 'post', 'string', JREQUEST_ALLOWHTML);
		$event->closing_date = JRequest::getVar('closing_date', NULL);
		$event->organiser_description = JRequest::getVar('organiser_description', NULL);
		CMealsEvent::saveEvent($event);
		// Save event group
		$eventGroup = new cmeals_std_class();
		$eventGroup->id = JRequest::getVar('group_id', NULL);
		$eventGroup->description_html = JRequest::getVar('group_description_html', NULL, 'post', 'string', JREQUEST_ALLOWHTML);
		$eventGroup->accountant_description = JRequest::getVar('group_accountant_description', NULL);
		CMealsEvent::saveEventGroup($eventGroup);
		// Done
		$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
	}
	
	/**
	 * Method to save status of an event from the signup report
	 * (only permitted for crew members)
	 */
	private function saveEventFromSignUpReport() {
		global $option;
		// Save event
		$eventId = JRequest::getVar('event_id', NULL);
		$groupId = JRequest::getVar('group_id', NULL);
		$status = JRequest::getVar('status', NULL);
		$tpl = JRequest::getVar('tpl', NULL);
		CMealsEvent::updateEventStatus($status, $groupId, $eventId);
		if (is_null($eventId)) {
			// Go to default signupreport
			$this->setRedirect("index.php?option=".$option."&view=signupreport&group_id=".$groupId.(is_null($tpl)?"":"&tpl=".$tpl));
		}
		else {
			// Go to default signupreport
			$this->setRedirect("index.php?option=".$option."&view=signupreport&group_id=".$groupId."&event_id=".$eventId.(is_null($tpl)?"":"&tpl=".$tpl));
		}
	}
	
	/**
	 * Method to save signups for an event (for the logged in member)
	 */
	private function saveSignUpList() {
		global $option;
		$signUp = new cmeals_std_class();
		$signUp->member_id = CMealsFrontEndUtils::getCurrentMemberId();
		$signUp->group_id = JRequest::getVar('group_id', NULL);
		$signUp->event_id = JRequest::getVar('event_id', NULL);
		$partVarNameArray = explode(",", JRequest::getVar('participant_vars', ""));
		foreach ($partVarNameArray as $partVarName) {
			$signUp->category = str_replace("part_", "", $partVarName);
			$signUp->participants = JRequest::getVar($partVarName, NULL);
			CMealsSignup::saveSignUp($signUp);
		}
		$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
	}
	
	/**
	 * Method to save automatic signups for an event type (for the logged in member)
	 */
	private function saveAutoSignUpList() {
		global $option;
		$autoSignUp = new cmeals_std_class();
		$autoSignUp->member_id = CMealsFrontEndUtils::getCurrentMemberId();
		$autoSignUp->event_type = JRequest::getVar('event_type', NULL);
		$partVarNameArray = explode(",", JRequest::getVar('participant_vars', ""));
		foreach ($partVarNameArray as $partVarName) {
			// break up 'part_MO_A1' into 'MO' and 'A1'
			$subTypeAndCategory = explode("_", str_replace("part_", "", $partVarName));
			$autoSignUp->event_subtype = $subTypeAndCategory[0];
			$autoSignUp->category = $subTypeAndCategory[1];
			$autoSignUp->participants = JRequest::getVar($partVarName, NULL);
			CMealsSignup::saveAutoSignUp($autoSignUp);
		}
		$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
	}
	
	/**
	 * Method to cancel
	 */
	public function cancel() {
		global $option;
		$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
	}
	
	/**
	 * Method to display the view
	 * @access public
	 */
	public function display() {
		
		// Do cleanup once every session (smaller cleanup than in admin controller)
		$session = JFactory::getSession();
		$cleanupDone = $session->get( "cleanupDone", false, "cmeals" );
		if (!$cleanupDone) {
			CMeals::archiveEvents();
			$session->set( "cleanupDone", true, "cmeals" );
		}
		
		$viewName = JRequest::getVar('view', NULL);
		
		if (is_null($viewName)) {
			$this->setRedirect("index.php?option=com_communalmeals&view=eventgroups");
			$this->redirect();
		}
		
		parent::display();
	}

}